package com.masterclient.app.businessmodule.minemodule.address;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.masterclient.app.R;
import com.eoner.baselibrary.bean.address.City;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.widget.address.AddressSelector;
import com.eoner.baselibrary.widget.address.CityInterface;
import com.eoner.baselibrary.widget.address.OnItemClickListener;

import java.util.ArrayList;

/**
 * 作者：sean
 * 创建时间：2017/7/31 14:03
 * 功能说明：
 */

public class AddressSelectDialog extends Dialog {

    private String isPurchase = "0";
    AddressSelector addressSelector;

    public AddressSelectDialog(@NonNull Context context) {
        super(context);
    }

    public AddressSelectDialog(@NonNull Context context, String isPurchase) {
        super(context);
        this.isPurchase = isPurchase;
    }

    private ArrayList<City> cities1, cities2, cities3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choose_address);

        addressSelector = (AddressSelector) findViewById(R.id.address);

        if ("1".equals(isPurchase)){
            addressSelector.setLineColor(Color.parseColor("#FF5000"));
            addressSelector.setTextSelectedColor(Color.parseColor("#FF5000"));
            addressSelector.setListTextSelectedColor(Color.parseColor("#FF5000"));
        }else {
            addressSelector.setLineColor(Color.parseColor("#333333"));
            addressSelector.setTextSelectedColor(Color.parseColor("#333333"));
            addressSelector.setListTextSelectedColor(Color.parseColor("#333333"));
        }
        addressSelector.setGrayLineColor(Color.parseColor("#ffffff"));
        addressSelector.setTextEmptyColor(Color.parseColor("#141922"));
        addressSelector.setListTextNormalColor(Color.parseColor("#141922"));

        addressSelector.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void itemClick(AddressSelector addressSelector, CityInterface cityInterface, int tabPosition) {
                switch (tabPosition) {
                    case 0:
                        if (onSelectListener != null) {
                            onSelectListener.onSelect("4", ((City) cityInterface).getCityId(), ((City) cityInterface).getName());
                        }
                        break;
                    case 1:
                        if (onSelectListener != null) {
                            onSelectListener.onSelect("5", ((City) cityInterface).getCityId(), ((City) cityInterface).getName());
                        }
                        break;
                    case 2:
                        if (onSelectListener != null) {
                            onSelectListener.onSelected(((City) cityInterface).getCityId(), ((City) cityInterface).getName());
                        }

//                      Toast.makeText(MainActivity.this,"tabPosition ："+tabPosition+" "+city.getCityName(),Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

        addressSelector.setOnTabSelectedListener(new AddressSelector.OnTabSelectedListener() {
            @Override
            public void onTabSelected(AddressSelector addressSelector, AddressSelector.Tab tab) {
                switch (tab.getIndex()) {
                    case 0:
                        addressSelector.setCities(cities1);
                        break;
                    case 1:
                        addressSelector.setCities(cities2);
                        break;
                    case 2:
                        addressSelector.setCities(cities3);
                        break;
                }
            }

            @Override
            public void onTabReselected(AddressSelector addressSelector, AddressSelector.Tab tab) {
            }
        });
        ImageView ivClose = (ImageView) findViewById(R.id.iv_close);
        ivClose.setOnClickListener(v -> dismiss());
    }


    public void setCities(ArrayList<City> list) {
        if (list != null && list.size() > 0 && "3".equals(list.get(0).getGrade())) {
            cities1 = list;
        }
        if (list != null && list.size() > 0 && "4".equals(list.get(0).getGrade())) {
            cities2 = list;
        }

        if (list != null && list.size() > 0 && "5".equals(list.get(0).getGrade())) {
            cities3 = list;
        }
        addressSelector.setCities(list);
    }

    public OnSelectListener onSelectListener;

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    public interface OnSelectListener {
        public void onSelect(String position, String id, String name);

        public void onSelected(String id, String name);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        window.setWindowAnimations(R.style.bottomShow);
        WindowManager.LayoutParams windowparams = window.getAttributes();
        window.setGravity(Gravity.BOTTOM);
        windowparams.width = ScreenUtils.getScreenWidth();
        windowparams.height = (int) (ScreenUtils.getScreenHeight() * 0.6f);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }
}
